package org.esprit.ocm.client.ui.one;

import org.esprit.ocm.client.rpc.oca.VirtualNetworkOcaService;
import org.esprit.ocm.client.rpc.oca.VirtualNetworkOcaServiceAsync;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.dto.impl.UserDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;

public class CreateVN extends Window {
	
	private final VirtualNetworkOcaServiceAsync	vnetOcaService	= GWT.create(VirtualNetworkOcaService.class);

	public CreateVN(final ServerDto server, final AwsCredentialsDto credentials, UserDto user) {
		setTitle("Create VN");
		setWidth(300);
		setHeight(250);
		setIsModal(true);
		setShowModalMask(true);
		setShowCloseButton(true);
		setShowMinimizeButton(false);
		centerInPage();

		final DynamicForm form = new DynamicForm();
		form.setWidth(250);
		form.setHeight(250);
		form.setPadding(5);
		form.setLayoutAlign(VerticalAlignment.TOP);

		TextItem name = new TextItem("name");
		name.setTitle("Name");
		name.setRequired(true);
		
		final TextAreaItem template = new TextAreaItem();  
		template.setTitle("Template");  

//		TextItem bridge = new TextItem("bridge");
//		bridge.setTitle("Bridge");
//		bridge.setRequired(true);
//
//		RadioGroupItem RG1 = new RadioGroupItem();
//		RG1.setTitle("Network type");
//		RG1.setRequired(true);
//		RG1.setVertical(true);
//		RG1.setValueMap("Fixed network", "Ranged network");
//		RG1.setRedrawOnChange(true);
//
//		TextItem lease = new TextItem("lease");
//		lease.setTitle("Lease IP:");
//		lease.setRequired(true);
//
//		TextAreaItem current = new TextAreaItem("current");
//		current.setTitle("Current leases");
//		current.setRequired(true);

		ButtonItem createButton = new ButtonItem("create", "Create");
		createButton.setAlign(Alignment.RIGHT);
		createButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				
				String secret = credentials.getAwsAccessKeyId() + ":" + credentials.getSecretAccessKey();
				String endpoint = server.getIpServer();
				
				vnetOcaService.allocate(secret, endpoint, template.getValue().toString(), new AsyncCallback<String>() {
					
					public void onSuccess(String s) {
						
						SC.warn(s);
						destroy();
					}
					
					public void onFailure(Throwable arg0) {
						
					}
				});
				destroy();
			}
		});

		form.setFields(name, template, createButton);

		form.redraw();

		addItem(form);
		draw();

	}

}
